\author {Sven Goldt}
\title {The Linux Programmer's Guide}

\chapter {Programming I/O ports}

Usually a PC at least has 2 serial and 1 parallel interfaces.
These interfaces are special devices and are mapped as follows:
\begin {itemize}
\item $/dev/ttyS0 - /dev/ttyS{\bf n}$\\
these are the RS232 serial devices 0-{\bf n} where {\bf n} depends on your hardware.
\item $/dev/cua0 - /dev/cua{\bf n}$\\
these are the RS232 serial devices 0-{\bf n} where {\bf n} depends on your hardware.
\item $/dev/lp0 - /dev/lp{\bf n}$\\
these are the parallel devices 0-{\bf n} where {\bf n} depends on your hardware.
\item $/dev/js0 - /dev/js{\bf n}$\\
these are the joystick devices 0-{\bf n} where $ 0<= {\bf n} <= 1 $.
\end {itemize}

The difference between the $/dev/ttyS*$ and $/dev/cua*$ devices is how
a call to open() is handled. The $/dev/cua*$ devices are supposed to be used
as callout devices and thus get other default settings by a call to open()
than the $/dev/ttyS*$ devices which will be initalized for incoming and
outgoing calls. By default the devices are controlling devices for the
process that opened them.
Normally ioctl() requests should handle all these special devices, but
POSIX preferred to define new functions to handle asynchronous
terminals heavily depending on the struct termios. Both methods require
including $<termios.h>$.\\
\begin {enumerate}
\item method ioctl:\\
TCSBRK, TCSBRKP, TCGETA (get attributes), TCSETA (set attributes)\\
Terminal I/O control (TIOC) requests:\\
TIOCGSOFTCAR (set soft carrier), TIOCSSOFTCAR (get soft carrier), 
TIOCSCTTY (set controlling tty), 
TIOCMGET (get modemlines), TIOCMSET (set modemlines), 
TIOCGSERIAL, TIOCSSERIAL, TIOCSERCONFIG, TIOCSERGWILD, TIOCSERSWILD, 
TIOCSERGSTRUCT, TIOCMBIS, TIOCMBIC, ...\\
\item method POSIX:\\
tcgetattr(), tcsetattr(), tcsendbreak(), tcdrain(), tcflush(), tcflow(), 
tcgetpgrp(), tcsetpgrp()\\
cfsetispeed(), cfgetispeed(), cfsetospeed(), cfgetospeed()\\
\item other methods:\\
outb,inb for hardware near programming like using the printer
port not for a printer.\\
\end {enumerate}
\clearpage

\section {Mouse Programming}
A mouse is either connected to a serial port or directly to the
AT bus and different types of mouse send distinct kinds of data, which makes
mouse programming a bit harder.
But, Andrew Haylett was so kind as to put a generous copyright on his
selection program which means you can use his mouse routines for
your own programs. Included in this guide you can find the pre-release
of selection-1.8 with the COPYRIGHT notice.
X11 already offers a comfortable mouse API, so Andrew's routines should
be used for non-X11 applications only.\\
You only need the modules mouse.c and mouse.h from the selection package.
To get mouse events you basically have to call ms\_init() and get\_ms\_event().
ms\_init needs the following 10 arguments:\\
\begin {enumerate}
\item {\it int acceleration}\\
is the acceleration factor. If you move the mouse more than
{\it delta} pixels, motion becomes faster depending on this value.
\item {\it int baud} \\
is the bps rate your mouse uses (normally 1200).
\item {\it int delta}\\
this is the number of pixels that you have to move the mouse before
the acceleration starts.
\item {\it char *device}\\
is the name of your mouse device (e.g. /dev/mouse).
\item {\it int toggle}\\
toggle the DTR, RTS or both DTR and RTS mouse modem lines on 
initialization (normally 0).
\item {\it int sample}\\
the resolution (dpi) of your mouse (normally 100).
\item {\it mouse\_type mouse}\\
the identifier of the connected mouse like P\_MSC (Mouse Systems Corp.)
for my mouse ;).
\item {\it int slack}\\
amount of slack for wraparound which means if slack is -1 a try to move the
mouse over the screen border will leave the mouse at the border.
Values $>=$ 0 mean that the mouse cursor will wrap to the other end after
moving the mouse {\it slack} pixels against the border.
\item {\it int maxx}\\
the resolution of your current terminal in x direction. With the 
default font, a char is 10 pixels wide, and therefore the overall
x screen resolution is 10*80-1.
\item {\it int maxy}\\
the resolution of your current terminal in y direction. With the 
default font, a char is 12 pixels high and therefore the overall
y screen resolution is 12*25-1.
\end {enumerate}
get\_ms\_event() just needs a pointer to a struct ms\_event.
If get\_ms\_event() returns -1, an error occured. On success, it
returns 0, and the struct ms\_event will contain the actual mouse state.


\section {Modem Programming}
See example miniterm.c\\
Use termios to control rs232 port.\\
Use Hayes Commands to control modem.

\section {Printer Programming}
See example checklp.c\\
Don't use termios to control printer port.
Use ioctl and inb/outb if necessary.\\
Use Epson, Postscript, PCL, etc. commands to control printer.\\
$<linux/lp.h>$\\
ioctl calls: LPCHAR, LPTIME, LPABORT, LPSETIRQ, LPGETIRQ, LPWAIT\\
inb/outb for status and control port.

\section {Joystick Programming}
See example js.c in the joystick loadable kernel module package.\\
$<linux/joystick.h>$\\
ioctl calls: JS\_SET\_CAL, JS\_GET\_CAL, JS\_SET\_TIMEOUT, JS\_GET\_TIMEOUT,
JS\_SET\_TIMELIMIT, JS\_GET\_TIMELIMIT, JS\_GET\_ALL, JS\_SET\_ALL.
A read operation on /dev/js{\bf n} will return the struct JS\_DATA\_TYPE.

\endinput
